package com.cloud.wyf.Controller;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.cloud.wyf.Entity.*;
import com.cloud.wyf.Entity.Dto.MiddelDto;
import com.cloud.wyf.Service.UserService;
import com.cloud.wyf.Util.GetTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/vote")
public class RelateVoteController {

    @Autowired
    UserService us;

    /**
     * 获得需要投票的中层干部
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/needVote")
    public String atainNeedVote(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //User user=(User)request.getSession().getAttribute("user");
        String uid = GetTokenUtil.getInfoFromToken(request.getHeader("x-access-token"), "id");
        User user = us.selectUserById(uid);
        List<Middel> list=new ArrayList<>();
        List<Part> all = us.selectByDirectPart("机关党委");
        String str="";
        for(Part part:all){
            str=str+part.getPart()+";";//得到所有属于机关党委的部门
        }
        if(user.getRole()==1){//领导
            Leader leader = us.selectLeaderById(user.getId());
            list = atainLeaderNeedVote(leader,str);
        }
//        else if(user.getRole()==2){//中层干部
//            Middel middel = us.selectMidById(user.getId());
//            list=atainMiddelNeedVote(middel,str);
//        }
        else if(user.getRole()==3){//群众
            Ordinary ordinary = us.selectOrdinaryById(user.getId());
            list=atainOrdinaryNeedVote(ordinary,str);
        }
       return JSONObject.toJSONString(list);
    }

    /**
     * 获得需要投票的领导班子
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/needLeaderShip")
    public String atainNeedLeaderShip(HttpServletRequest request, HttpServletResponse response){
        if(request.getHeader("x-access-token")!=null&&!"".equals(request.getHeader("x-access-token"))) {
            String uid = GetTokenUtil.getInfoFromToken(request.getHeader("x-access-token"), "id");
            User user = us.selectUserById(uid);
            if (user != null) {
                List<LeaderShip> leaderShips = us.selectAllLeaderShip();
                return JSONObject.toJSONString(leaderShips);
            }else return "you have no power";
        }else {
            User user = (User)request.getSession().getAttribute("user");
            if(user!=null) {
                if (user.getRole() == 1) return JSONObject.toJSONString(us.selectAllLeaderShip());
                else return "you have no power";
            }else return JSONObject.toJSONString(us.selectAllLeaderShip());
        }
    }

    /**
     * 判断是否可以投票
     * @param request
     * @return
     */
    @RequestMapping("/isAuth")
    public String isAuth(HttpServletRequest request){
        String uid = GetTokenUtil.getInfoFromToken(request.getHeader("x-access-token"), "id");
        int role = Integer.valueOf(GetTokenUtil.getInfoFromToken(request.getHeader("x-access-token"), "role"));
       if(role==1){
           Leader leader = us.selectLeaderById(uid);
           if(leader.getStatus()==0) return "no";
           else return "yes";
       }
//       else if(role==2){
//           Middel middel = us.selectMidById(uid);
//           if(middel.getStatus()==0) return "no";
//           else return "yes";
//       }
        else if(role==3){
           Ordinary ordinary = us.selectOrdinaryById(uid);
           if(ordinary.getStatus()==0) return "no";
           else return "yes";
       }else return "You need not to vote";
    }

    /**
     * 开始投票
     * @return
     */
    @RequestMapping("/startVote")
    public String startVote(){
        Boolean isStart = us.startVote(1,  1);
        if(isStart) return JSONObject.toJSONString(Message.success("success to start vote"));
        else return JSONObject.toJSONString(Message.failure("failure to start vote"));
    }

    /**
     * 关闭投票
     * @return
     */
    @RequestMapping("/closeVote")
    public String closeVote(){
        Boolean isStart = us.startVote(0,0);
        if(isStart) return JSONObject.toJSONString(Message.success("success to close vote"));
        else return JSONObject.toJSONString(Message.failure("failure to close vote"));
    }

    public List<Middel> atainLeaderNeedVote(Leader leader,String str){
        List<Middel> result=new ArrayList<>();
        List<Middel> allMiddel = us.selectAllMi();
        for(Middel m:allMiddel) {
            if(str.contains(m.getPart())){//属于机关党委
                if(m.getPosition().equals("正县")){
                    result.add(m);
                }else {
                    result.add(m);
                }
            }else {//不属于机关党委
                if(m.getPosition().equals("正县")){
                    result.add(m);
                }else {
                    if (leader.getManagepart()!=null&&!"".equals(leader.getManagepart())) {
                        if (leader.getManagepart().contains(m.getPart())) {
                            result.add(m);
                        }
                    }
                }
            }
        }
        return result;
    }

    public List<Middel>  atainOrdinaryNeedVote(Ordinary ordinary,String str){
        List<Middel> result=new ArrayList<>();
        List<Middel> allMiddel = us.selectAllMi();
        for(Middel m:allMiddel){
            if(str.contains(m.getPart())) {//属于机关党委
                if (str.contains(ordinary.getPart())) {//属于机关党委群众
                    result.add(m);
                }
            }else {//不属于机关党委
                System.out.println(m.toString());
                String middelDirectPart = us.selectByPart(m.getPart()).getDirectparty();
                System.out.println(ordinary.getPart());
                String ordinaryDirectPart = us.selectByPart(ordinary.getPart()).getDirectparty();
                if(ordinaryDirectPart.equals(middelDirectPart)){
                    result.add(m);
                }
            }
        }
        return result;
    }

//    public List<Middel>  atainMiddelNeedVote(Middel ordinary,String str){
//        List<Middel> result=new ArrayList<>();
//        List<Middel> allMiddel = us.selectAllMi();
//        for(Middel m:allMiddel){
//            if(str.contains(m.getPart())) {//属于机关党委
//                if (str.contains(ordinary.getPart())) {//属于机关党委群众
//                    result.add(m);
//                }
//            }else {//不属于机关党委
//                String middelDirectPart = us.selectByPart(m.getPart()).getDirectpart();
//                String ordinaryDirectPart = us.selectByPart(ordinary.getPart()).getDirectpart();
//                if(ordinaryDirectPart.equals(middelDirectPart)){
//                    result.add(m);
//                }
//            }
//        }
//        return result;
//    }

}
